#include "Window.h"

namespace DinoUI
{
	Window::Window(Window* parent)
	{
		SetActive(true);
		parent->SetChildWidget(this);
	}

	Window::Window(void)
	{
		SetActive(true);
		Widget::GetEventDispatcher().Connect(this);
	}


	Window::~Window(void)
	{
	}

	void Window::SetChildWidget(Window *wnd)
	{
		m_childWindows.push_back(wnd);
	}

	void Window::SetPosition(int x, int y)
	{
		int dispX = x - GetX();
		int dispY = y - GetY();

		Rect::SetPosition(x, y);

		for(windowIt i = m_childWindows.begin(), c = m_childWindows.end(); i<c; i++)
		{
			(*i)->SetPosition((*i)->GetX() + dispX, (*i)->GetY() + dispY);
		}
	}

	bool Window::HandleEvent(WidgetEvent *evt)
	{
		// exit from func if widget is inActive
		if(IsActive() == false)
			return false;

		// handle self events
		switch(evt->eventType)
		{
		case WIDGETEVENT_TOUCH:
			if(IsPointInside(evt->x, evt->y) == true)
			{
				//SetPosition(GetX() + 5, GetY() + 5);
				//return true;
			}			
			break;
		}

		// handle child events
		for(windowIt i = m_childWindows.begin(), c = m_childWindows.end(); i<c; i++)
		{
			if((*i)->HandleEvent(evt) == true)
			{
				return true;
			}
		}

		return false;
	}

	void Window::Draw()
	{
		//

		for(windowIt i = m_childWindows.begin(), c = m_childWindows.end(); i<c; i++)
		{
			(*i)->Draw();
		}
	}
}

